const { db } = require('../../../conf');
const dbutil = require('../../../libs/MyDB');
const uuid = require('node-uuid');

module.exports = {
    getRowInfo: async function () {
        let rows = [];
        let sql = `SELECT *,COUNT(r.row_id) count from res_tabletype t LEFT JOIN res_row r on t.tabletype_id = r.tabletype_id GROUP BY tabletype_people ORDER BY t.tabletype_id`;
        await dbutil.sqlSelect(sql).then(
            res => {
                rows = res;
            }
        )
        console.log(rows);
        return {
            code: 0,
            msg: "查询成功",
            data: rows,
            count: rows.length,
        }
    },
    next: async function(data){
        let tables =[];
        await dbutil.multiSelect('res_table','table_id',`tabletype_id=${data.type} and table_usage = '未使用'`)
        .then(
            res=>{
                tables=res;
            }
        )
        if(tables.length==0){
            return {
                code: 400,
                msg: "执行失败！没有空闲桌位！",
            }
        }
        await dbutil.del('res_row',`row_number='${data.num}'`)
        let tableId;
        await dbutil.update('res_table',`tabletype_id=${data.type} and table_usage = '未使用' limit 1`,{user_id:data.userid,table_usage:'使用中'})
        .then(
            res =>{
                tableId = res.insertId;
            }
        )
        let order_number = uuid.v1();
        let order = {
            order_number:order_number,
            order_payment_status:'未提交',
            user_id:data.userid,
            table_id:tableId
        };
        let orderObj = {}
        await dbutil.add('res_order',order)
        return {
            code: 200,
            msg: "执行成功",
        }
    }
}